package 并发编程13.同步器;

/**
 * @author idea
 * @data 2019/2/1
 */
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        final Object obj = new Object();
        Thread A = new Thread(new Runnable() {
            @Override
            public void run() {
                    int sum = 0;
                    for(int i=0;i<10;i++){
                        sum+=i;
                    }
                    try {
                        synchronized (obj) {
                            obj.wait();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    System.out.println(sum);
            }
        });
        A.start();
        //睡眠一秒钟，保证线程A已经计算完成，阻塞在wait方法(注意wait和notify需要在同步块里面进行调用)
        Thread.sleep(1000);
        synchronized (obj){
            obj.notify();
        }
    }
}
